home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 79 / maccd 79.iso / multimedial / GL Tron / Source / gltron / fonttex.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-05-13  |  4.4 KB  |  171 lines  |  [TEXT/CWIE]

  1. #include "gltron.h"
  2.  
  3. #define FTX_ERR "[fonttex error]: "
  4. extern char *getFullPath(char*);
  5.  
  6. void getLine(char *buf, int size, FILE *f) {
  7.   do {
  8.     fgets(buf, size, f);
  9.   } while( buf[0] == '\n' || buf[0] == '#');
  10. }
  11.  
  12. void fbmpUnloadFont(fontbmp *fbmp) {
  13.   freeTextureData(fbmp->tex);
  14.   free(fbmp);
  15. }
  16.  
  17. fontbmp* fbmpLoadFont(char *filename) {
  18.   char *path;
  19.   FILE *file;
  20.   char buf[100];
  21.   int len;
  22.   fontbmp *fbmp;
  23.   
  24.   path = getFullPath(filename);
  25.   if(path == NULL) {
  26.     fprintf(stderr, FTX_ERR "can't load font file '%s'\n", filename);
  27.     return NULL;
  28.   }
  29.   file = fopen(path, "r");
  30.   free(path);
  31.  
  32.   fbmp = (fontbmp*) malloc(sizeof(fontbmp));
  33.   /* bitmap texture width, character width */
  34.   getLine(buf, sizeof(buf), file);
  35.   sscanf(buf, "%d %d ", &(fbmp->texwidth), &(fbmp->width));
  36.   /* lowest character, highest character */
  37.   getLine(buf, sizeof(buf), file);
  38.   sscanf(buf, "%d %d ", &(fbmp->lower), &(fbmp->upper));
  39.   /* bitmap filename */
  40.   getLine(buf, sizeof(buf), file);
  41.   len = strlen(buf) + 1;
  42.   if(buf[len - 2] == '\n') buf[len - 2] = 0;
  43.   /* fbmp->bitmapName = (char*) malloc(len);
  44.      memcpy(fbmp->bitmapName, buf, len); */
  45.   fbmp->tex = loadTextureData(buf);
  46.  
  47.   fclose(file);
  48.   return fbmp;
  49. }
  50.  
  51. fonttex *ftxLoadFont(char *filename) {
  52.   char *path;
  53.   FILE *file;
  54.   char buf[100];
  55.  
  56.   int i;
  57.   int len;
  58.   fonttex *ftx;
  59.   
  60.   path = getFullPath(filename);
  61.   if(path == NULL) {
  62.     fprintf(stderr, FTX_ERR "can't load font file '%s'\n", filename);
  63.     return NULL;
  64.   }
  65.   file = fopen(path, "r");
  66.   free(path);
  67.  
  68.   /* TODO(5): check for EOF errors in the following code */
  69.   
  70.   /* nTextures, texture width, char width */
  71.   ftx = (fonttex*) malloc(sizeof(fonttex));
  72.   getLine(buf, sizeof(buf), file);
  73.   sscanf(buf, "%d %d %d ", &(ftx->nTextures), &(ftx->texwidth), &(ftx->width));
  74.   /* lowest character, highest character */
  75.   getLine(buf, sizeof(buf), file);
  76.   sscanf(buf, "%d %d ", &(ftx->lower), &(ftx->upper));
  77.   /* font name */
  78.   getLine(buf, sizeof(buf), file);
  79.   len = strlen(buf) + 1;
  80.  
  81.   ftx->fontname = malloc(len);
  82.   memcpy(ftx->fontname, buf, len);
  83.  
  84.   /* prepare space for texture IDs  */
  85.   ftx->texID = (unsigned int*) malloc(ftx->nTextures * sizeof(unsigned int));
  86.   glGenTextures(ftx->nTextures, ftx->texID);
  87.  
  88.   /* the individual textures */
  89.   for(i = 0; i < ftx->nTextures; i++) {
  90.     char *texname;
  91.     getLine(buf, sizeof(buf), file);
  92.     len = strlen(buf) + 1;
  93.     if(buf[len - 2] == '\n') buf[len - 2] = 0;
  94.     texname = (char*)malloc(len);
  95.     memcpy(texname, buf, len); 
  96.     glBindTexture(GL_TEXTURE_2D, ftx->texID[i]);
  97.     loadTexture(texname, GL_RGBA);
  98.     free(texname);
  99.  
  100.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  101.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  102.     /* glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); */
  103.     /* glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); */
  104.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  105.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  106.   }
  107.  
  108.   fclose(file);
  109.   return ftx;
  110. }
  111.  
  112. void ftxUnloadFont(fonttex *ftx) {
  113.   glDeleteTextures(ftx->nTextures, ftx->texID);
  114.  
  115.   free(ftx->texID);
  116.   free(ftx->fontname);
  117.   free(ftx);
  118. }
  119.  
  120. void ftxRenderString(fonttex *ftx, char *string, int len) {
  121.   int i;
  122.   int bound = -1;
  123.   int index;
  124.   
  125.   int tex;
  126.   int w;
  127.   float cw;
  128.   float cx, cy;
  129.  
  130.   w = ftx->texwidth / ftx->width;
  131.   cw = (float)ftx->width / (float)ftx->texwidth;
  132.  
  133.   for(i = 0; i < len; i++) {
  134.     /* find out which texture it's in */
  135.     /* TODO(4): find out why the +1 is necessary */
  136.     index = string[i] - ftx->lower + 1;
  137.     if(index >= ftx->upper) 
  138.       fprintf(stderr, FTX_ERR " index out of bounds");
  139.     tex = index / (w * 
  140. w);
  141.     /* bind texture */
  142.     if(tex != bound) {
  143.       glBindTexture(GL_TEXTURE_2D, ftx->texID[tex]);
  144.       glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  145.       bound = tex;
  146.     }
  147.     /* find texture coordinates */
  148.     index = index % (w * w);
  149.     cx = (float)(index % w) / (float)w;
  150.     cy = (float)(index / w) / (float)w;
  151.     /* draw quad */
  152.     /* fprintf(stderr, FTX_ERR "coords: tex %d (%.2f, %.2f), %.2f\n", */
  153.     /*     bound, cx, cy, cw); */
  154.  
  155.     glBegin(GL_QUADS);
  156.     glTexCoord2f(cx, 1 - cy - cw);
  157.     glVertex2f(i, 0);
  158.     glTexCoord2f(cx + cw, 1 - cy - cw);
  159.     glVertex2f(i + 1, 0);
  160.     glTexCoord2f(cx + cw, 1 - cy);
  161.     glVertex2f(i + 1, 1);
  162.     glTexCoord2f(cx, 1 - cy);
  163.     glVertex2f(i, 1);
  164.     glEnd();
  165.   }
  166.   /* checkGLError("fonttex.c ftxRenderString\n"); */
  167. }
  168.  
  169.  
  170.  
  171.